﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class ABFOLGEController
    {
        [ChildActionOnly]
        public ActionResult SAMBUSY(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            if (!MABF.IsAMReady)
            {
                INFO = "请扫描有效的设备";
            }
            return PartialView(MABF);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SAMBUSY(ABFOLGEModel model, string returnUrl)
        {
            try
            {
                ModelState.Clear();
                AMABF(model);
            }
            catch (Exception ex)
            {
                INFO = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void AMABF(ABFOLGEModel model)
        {

            MABF.IsAMReady = false;
            if (model.MACHINEID == null)
                return;
            var result = AMS.Where(p => p.MACHINEID.Trim() == model.MACHINEID.Trim()).OrderByDescending(p => p.MACHINEID);
            int remain = result.Count();
            foreach (var m in result)
            {
                MABF.MACHINEID = m.MACHINEID;
                MABF.MAM= m; 
                MABF.IsAMReady = true;
            }
            PM["MKRS"] = MABF.MAM.MKRS;
            PM["MLFD"] = MABF.MAM.MLFD;
            PM["MPOS"] = MABF.MAM.MPOS;
            SReult = ABP.LOADBYAM(PM);
            LOADABFS(SReult.Tables[0]);
            if (ABFS.Count > 0)
            {
                if (MABF.MAM.MACHINEID.Trim().Length > 1)
                {
                    ABFS[0].MAM = MABF.MAM;
                }
                MABF = ABFS[0];
                MABF.IsABPReady = true;
                STEPCHK(MABF);                
            }
        }
    }
}
